package com.hxx.day6;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class TestDemo2 {
    @Test
    public void test1(){
        //日期时间
        System.out.println(LocalDateTime.now());
    }
    @Test
    public void test2(){
        //日期
        System.out.println(LocalDate.now());
        // 时间
        System.out.println(LocalTime.now());
    }

    /**
     * 格式化日期
     */
    @Test
    public void test3(){
        //DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt.format(formatter));
    }
    @Test
    public void test4(){
        //指定日期和时间
        LocalDateTime ldt = LocalDateTime.of(2017,06, 23, 12, 11);
        System.out.println(ldt);
    }
    /**
     * 日期计算
     */
    @Test
    public void test5(){
        LocalDateTime ldt = LocalDateTime.now();
        //加
        System.out.println(ldt.plusDays(1));
        System.out.println(ldt.plusYears(1));
        //减
        System.out.println(ldt.minusMonths(1));
        //获取
        System.out.println(ldt.getDayOfMonth());
    }
    /**
     * 时间戳
     */
    @Test
    public void test6(){
        System.out.println(System.currentTimeMillis());
        //获取时间戳,
        Instant ins = Instant.now();
        //默认获取utc时区的时间
        System.out.println(ins);
        //和我们时区差8个小时，调整一下
        OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8));
        System.out.println(odt);
    }
    /**
     * 时间计算
     */
    @Test
    public void test7(){
        //计算时间差
        LocalDateTime ldt = LocalDateTime.of(2017,06, 23, 7, 11);
        LocalDateTime ldt1 = LocalDateTime.of(2017,06, 23, 12, 11);
        Duration time = Duration.between(ldt, ldt1);
        System.out.println("获取秒差："+time.getSeconds());
        System.out.println("获取毫秒差："+time.toMillis());
    }
    /**
     * 日期差计算
     */
    @Test
    public void test8(){
        //计算日期差
        LocalDate ldt = LocalDate.of(2017,04, 21);
        LocalDate ldt1 = LocalDate.of(2017,06, 23);
        Period date = Period.between(ldt, ldt1);
        System.out.println("获取天差："+date.getDays());
        System.out.println("获取月差："+date.toTotalMonths());
    }
    /**
     * 时间校正
     */
    @Test
    public void test9(){
        //时间校正器
        LocalDateTime ldt = LocalDateTime.now();
        //获取下一天或年、月等
        LocalDateTime ldt1 = ldt.with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(ldt1);
        //获取下周日
        LocalDateTime ldt2 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ldt2);
    }
    /**
     * 查看时区
     */
    @Test
    public void test10(){
        //查看时区
        ZoneId.getAvailableZoneIds().forEach(System.out::println);
    }
    /**
     * 指定时区
     */
    @Test
    public void test11(){
        //指定时区
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(ldt);
    }

}






























